/*
 * listaAux.c
 *
 *  Created on: 05/11/2010
 *      Author: dela
 */

#include "listaAux.h"
#include "../domain/ListaSimple.h"
#include <string.h>
#include <malloc.h>
#include <stdbool.h>

/**
 *
 */
bool lsAux_insertOrderString( TListaSimple *ls, char *string ) {
	if ( ls_Vacia( *ls ) ) {
		ls_Insertar( ls, LS_PRIMERO, string );
		return true;
	} else {
		char *aux;
		ls_MoverCorriente( ls, LS_PRIMERO );
		ls_ElemCorriente( *ls, (void**)&aux );
		while ( strcasecmp( string, aux ) > 0 && ls_MoverCorriente( ls, LS_SIGUIENTE ) ) {
			ls_ElemCorriente( *ls, (void**)&aux );
		}
		if ( strcasecmp( string, aux ) < 0 ) {
			ls_Insertar( ls, LS_ANTERIOR, string );
			return true;
		} else if ( strcasecmp( string, aux ) > 0 ) {
			ls_Insertar( ls, LS_SIGUIENTE, string );
			return true;
		}
		return false;
	}
}

/**
 *
 */
bool lsAux_insertOrderPerson( TListaSimple *ls, regPer *reg ) {
	if ( ls_Vacia( *ls ) ) {
		ls_Insertar( ls, LS_PRIMERO, reg );
		return true;
	} else {
		regPer *aux;
		ls_MoverCorriente( ls, LS_PRIMERO );
		ls_ElemCorriente( *ls, (void**)&aux );
		while ( strcasecmp( reg->nombre, aux->nombre ) > 0 && ls_MoverCorriente( ls, LS_SIGUIENTE ) ) {
			ls_ElemCorriente( *ls, (void**)&aux );
		}
		if ( strcasecmp( reg->nombre, aux->nombre ) < 0 ) {
			ls_Insertar( ls, LS_ANTERIOR, reg );
			return true;
		} else if ( strcasecmp( reg->nombre, aux->nombre ) > 0 ) {
			ls_Insertar( ls, LS_SIGUIENTE, reg );
			return true;
		}
		return false;
	}
}

/**
 *
 */
bool lsAux_insertOrderNode( TListaSimple *ls, TNode *node ) {
	if ( ls_Vacia( *ls ) ) {
		ls_Insertar( ls, LS_PRIMERO, node );
		return true;
	} else {
		TNode *aux;
		ls_MoverCorriente( ls, LS_PRIMERO );
		ls_ElemCorriente( *ls, (void**)&aux );
		while ( node->id > aux->id && ls_MoverCorriente( ls, LS_SIGUIENTE ) ) {
			ls_ElemCorriente( *ls, (void**)&aux );
		}
		if ( node->id < aux->id ) {
			ls_Insertar( ls, LS_ANTERIOR, node );
			return true;
		} else if ( node->id > aux->id ) {
			ls_Insertar( ls, LS_SIGUIENTE, node );
			return true;
		}
		return false;
	}
}

/**
 *
 */
bool lsAux_insertOrderInt( TListaSimple *ls, int *num ) {
	if ( ls_Vacia( *ls ) ) {
		ls_Insertar( ls, LS_PRIMERO, num );
		return true;
	} else {
		int *aux;
		ls_MoverCorriente( ls, LS_PRIMERO );
		ls_ElemCorriente( *ls, (void**)&aux );
		while ( *num > *aux && ls_MoverCorriente( ls, LS_SIGUIENTE ) ) {
			ls_ElemCorriente( *ls, (void**)&aux );
		}
		if ( *num < *aux ) {
			ls_Insertar( ls, LS_ANTERIOR, num );
			return true;
		} else if ( *num > *aux ) {
			ls_Insertar( ls, LS_SIGUIENTE, num );
			return true;
		}
		return false;
	}
}

/**
 *
 */
int lsAux_getIdexOfString( TListaSimple *ls, char *string ) {
	if ( ls_Vacia( *ls ) ) {
		return -1;
	} else {
		int i = 0;
		char *aux;
		ls_MoverCorriente( ls, LS_PRIMERO );
		ls_ElemCorriente( *ls, (void**)&aux );
		while ( strcasecmp( string, aux ) != 0 && ls_MoverCorriente( ls, LS_SIGUIENTE ) ) {
			ls_ElemCorriente( *ls, (void**)&aux );
			i++;
		}
		if ( strcasecmp( string, aux ) == 0 ) {
			return i;
		} else {
			return -1;
		}
	}
}

/**
 *
 */
int lsAux_getIdexOfPerson( TListaSimple *ls, char *string ) {
	if ( ls_Vacia( *ls ) ) {
		return -1;
	} else {
		int i = 0;
		regPer *aux;
		ls_MoverCorriente( ls, LS_PRIMERO );
		ls_ElemCorriente( *ls, (void**)&aux );
		while ( strcasecmp( string, aux->nombre ) != 0 && ls_MoverCorriente( ls, LS_SIGUIENTE ) ) {
			ls_ElemCorriente( *ls, (void**)&aux );
			i++;
		}
		if ( strcasecmp( string, aux->nombre ) == 0 ) {
			return i;
		} else {
			return -1;
		}
	}
}

/**
 *
 */
int lsAux_getIdexOfInt( TListaSimple *ls, int *num ) {
	if ( ls_Vacia( *ls ) ) {
		return -1;
	} else {
		int i = 0;
		char *aux;
		ls_MoverCorriente( ls, LS_PRIMERO );
		ls_ElemCorriente( *ls, (void**)&aux );
		while ( *num != *aux && ls_MoverCorriente( ls, LS_SIGUIENTE ) ) {
			ls_ElemCorriente( *ls, (void**)&aux );
			i++;
		}
		if ( *num == *aux ) {
			return i;
		} else {
			return -1;
		}
	}
}
